﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using YemekteyizLib.Bs;
using YemekteyizLib;

public partial class main : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["uye"] != null)
        {
            SepetDoldur();

        }

        if (!IsPostBack)
        {
            if (Session["ilID"] != null)
            {
                string ilID = Session["ilID"].ToString();

                Il kolpa = new Il();
                kolpa.ID = Convert.ToInt32(ilID);

                drpIlceler.DataTextField = "Adi";
                drpIlceler.DataValueField = "ID";
                List<Ilce> ilceler = IlceProvider.getByIl(kolpa);
                ilceler.Sort();
                drpIlceler.DataSource = ilceler;



                drpRestoranlar.DataTextField = "Adi";
                drpRestoranlar.DataValueField = "ID";
                List<Restoran> restoranlar = RestoranProvider.GetByIl(kolpa);

                restoranlar.Insert(0, new Restoran { Adi = "-Tüm Restoranlar", ID = 0 });
                drpRestoranlar.DataSource = restoranlar;
                DataBind();

                if (Request.QueryString["ilceID"] != null)
                {
                    drpIlceler.SelectedValue = Request.QueryString["ilceID"];
                }
                else if (Request.QueryString["restID"] != null)
                {
                    drpRestoranlar.SelectedValue = Request.QueryString["restID"];
                    drpIlceler.SelectedValue = RestoranProvider.GetByID(Convert.ToInt32(drpRestoranlar.SelectedValue)).BulunduguIlce.ID.ToString();

                }

                Uye u = Session["uye"] as Uye;
                if (u != null)
                {
                    lblKullaniciBilgisi.Visible = true;
                    lblKullaniciBilgisi.Text = u.Adi + " " + u.Soyadi;
                }
                else
                {
                    LinkButton1.Visible = false;
                }
            }
            else
            {
                Response.Redirect("default.aspx");
            }
        }
        
    }

    public void SepetDoldur()
    {
        Uye u = Session["uye"] as Uye;

        u.AktifSepet = SepetProvider.GetAktifByUye(u);
        Session["uye"] = u;

        DataList1.DataSource = u.AktifSepet.Urunleri;
        DataList1.DataBind();
    }

    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        string ilceID = drpIlceler.SelectedValue;
        Response.Redirect("restoranlar.aspx?ilceID=" + ilceID);
    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        Session["uye"] = null;
        Response.Redirect(Request.RawUrl);
    }
    protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
    {
        switch (e.CommandName)
        {
            case "guncelle": break;

            case "onay":
                Response.Redirect("sepetOnay.aspx");

                break;

            default:
                break;
        }
    }
}
